package com.example.demo.controller;


import com.example.demo.entity.Achieve;
import com.example.demo.entity.User;
import com.example.demo.entity.dto.ResultBean;
import com.example.demo.exception.bizException.BizException;
import com.example.demo.exception.bizException.BizExceptionCodeEnum;
import com.example.demo.service.AchieveService;
import com.example.demo.util.Role;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *成果
 * @author cy
 * @since 2020-08-01
 * @version v1.0
 */
@RestController
@RequestMapping("/achieve")
public class AchieveController {

    @Autowired
    private AchieveService achieveService;

    /**
    * 查询分页数据
    */
    @RequestMapping(method = RequestMethod.GET,value = "/normal")
    public ResultBean<?> listByPage(@RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(name = "userId", defaultValue = "",required = false) Integer userId,
                                    @RequestParam(name = "achieveName", defaultValue = "") String  achieveName,
                                    @RequestParam(name = "achieveType", defaultValue = "") String  achieveType,
                                    @RequestParam(name = "achieveIndustry", defaultValue = "") String  achieveIndustry,
                                    @RequestParam(name = "state", defaultValue = "") Integer  state) {
        return new ResultBean<>(achieveService.listAchievesByPage(page, pageSize,userId,achieveName,state,achieveType,achieveIndustry));
    }


    /**
    * 根据id查询
    */
    @RequestMapping(method = RequestMethod.GET, value = "/normal/{id}")
    public ResultBean<?> getById(@PathVariable("id") Integer id) {
        return new ResultBean<>(achieveService.getAchieveById(id));
    }

    /**
    * 新增
    */
    @RequestMapping(method = RequestMethod.POST)
    @RequiresRoles({Role.TEAM})
    public ResultBean<?> insert(@RequestBody Achieve achieve) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        achieve.setUId(user.getUserId());
        return new ResultBean<>(achieveService.insertAchieve(achieve));
    }

    /**
    * 删除
    */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    @RequiresRoles({Role.TEAM})
    public ResultBean<?> deleteById(@PathVariable("id") Integer id) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        return new ResultBean<>(achieveService.deleteAchieveById(id,user.getUserId()));
    }

    /**
    * 修改
    */
    @RequestMapping(method = RequestMethod.PUT)
    @RequiresRoles({Role.TEAM})
    public ResultBean<?> updateById(@RequestBody Achieve achieve) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        achieve.setUId(user.getUserId());
        return new ResultBean<>(achieveService.updateAchieve(achieve));
    }

    /**
     * 管理员通过审核成果
     */
    @RequiresRoles(Role.ADMIN)
    @RequestMapping(method = RequestMethod.PUT, value = "/check")
    public ResultBean<?> checkAchieve(@RequestBody Achieve achieve) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null) {
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        return new ResultBean<>(achieveService.checkAchieve(achieve));
    }

    /**
     * 导入成果
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.POST,value = "/importAchieve")
    public ResultBean<?> importAchieve(@RequestParam("achieve") MultipartFile file) throws IOException {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        String filename = file.getOriginalFilename();
        return new ResultBean<>(achieveService.importAchieve(filename,file,user));
    }

    /**
     *  查询未审核的成果数量
     */
    @RequestMapping(method = RequestMethod.GET,value = "/uncheckedNum")
    public  ResultBean<?> UncheckedNum(){
        return new ResultBean<>(achieveService.UncheckedNum());
    }
}
